package cn.hs.service.impl;

import cn.hs.entity.CourseMark;
import cn.hs.mapper.CourseMapper;
import cn.hs.mapper.CourseMarkMapper;
import cn.hs.mapper.SystemuserMapper;
import cn.hs.mapper.UserMapper;
import cn.hs.pojo.CourseMarkSelect;
import cn.hs.service.CourseMarkService;
import cn.hs.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CourseMarkServiceImpl
 * @Description TODO  课程评价业务层
 * @Author yun
 * @Date 2024年10月23日 17:57
 * @Version 1.0
 */
@Service
public class CourseMarkServiceImpl implements CourseMarkService {

    @Autowired
    private CourseMarkMapper mapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private SystemuserMapper systemuserMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result add(CourseMark mark) {
//        判断是否存在
        if(mark == null){
            return  Result.error();
        }
//        判断外键是否存在
//        courseID
        if(courseMapper.selectById(mark.getCourseId()) == null){
            return  Result.error("课程id不存在");
        }
//        userID
        if(systemuserMapper.selectById(mark.getUserId()) == null){
            return Result.error("用户id不存在");
        }
//        判断是否添加成功
        if(mapper.add(mark) == 1){
            return Result.success();
        }
        return Result.error();

    }

    @Override
    public Result delete(Integer id) {
        if(id == null){
            return  Result.error();
        }
        if(mapper.delete(id) == 1){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result deleteByCourseId(Integer id) {
        if(id == null){
            return  Result.error();
        }
        if(mapper.deleteByCourseId(id) == 1){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result update(CourseMark mark) {
        if(mark == null){
            return  Result.error();
        }
        //        判断外键是否存在
//        courseID
        if(courseMapper.selectById(mark.getCourseId()) == null){
            return  Result.error("课程id不存在");
        }
//        userID
        if(systemuserMapper.selectById(mark.getUserId()) == null){
            return Result.error("用户id不存在");
        }

        if(mapper.update(mark) == 1){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result selectAll(CourseMarkSelect select) {
        if(select != null){
            if(select.getPageNo() != null && select.getPageSize() != null){
                PageHelper.startPage(select.getPageNo(), select.getPageSize());
            }
        }
        List<CourseMark> list = mapper.selectAll(select);
//        添加userName;
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setUserName(userMapper.selectById(list.get(i).getUserId()).getUsername());
        }

//        添加courseName外键
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getCourseId() != null){
                list.get(i).setCourseName(courseMapper.selectNameById(list.get(i).getCourseId()));
            }
        }

        PageInfo<CourseMark> info = new PageInfo<>(list);
        return Result.success(info);
    }

    @Override
    public Result selectById(Integer id) {
        if(id == null){
            return  Result.error();
        }
        CourseMark mark = mapper.selectById(id);
        return Result.success(mark);

    }
}
